package com.gzjh_group.photoalbum.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.gzjh_group.photoalbum.DepthPageTransformer;
import com.gzjh_group.photoalbum.GuidePagerAdapter;
import com.gzjh_group.photoalbum.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GuideActivity extends AppCompatActivity {

    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.pointGroup)
    LinearLayout pointGroup;
    @BindView(R.id.btnEnter)
    Button btnEnter;
    @BindView(R.id.view_guide_point)
    View guidePoint;

    private int[] imgs = {R.mipmap.first, R.mipmap.second, R.mipmap.three};
    private List<ImageView> imageViewList = new ArrayList<>();
    private int pointWidth = 0;

    @OnClick
    public void onClick(View view) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);


        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) guidePoint.getLayoutParams();
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        params.leftMargin = params.leftMargin - 20;
        guidePoint.setLayoutParams(params);
        initData();

        GuidePagerAdapter adapter = new GuidePagerAdapter(imageViewList);
        viewPager.setAdapter(adapter);
        viewPager.setPageTransformer(true, new DepthPageTransformer());
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                int len = (int) (pointWidth * positionOffset + position * pointWidth);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) guidePoint.getLayoutParams();

                params.bottomMargin = 50;
                params.leftMargin = len;
                //设置选中圆点在父控件中的左间距

                guidePoint.setLayoutParams(params);
            }

            int lastPosition;

            @Override
            public void onPageSelected(int position) {
                // 页面被选中
                // 设置当前页面选中
                pointGroup.getChildAt(position).setSelected(true);
                // 设置前一页不选中
                pointGroup.getChildAt(lastPosition).setSelected(false);

                // 替换位置
                lastPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initData() {
        for (int i = 0; i < imgs.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setBackgroundResource(imgs[i]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            ViewGroup.LayoutParams layoutParams = new ViewPager.LayoutParams();
            imageView.setLayoutParams(layoutParams);
            imageViewList.add(imageView);


            ImageView point = new ImageView(this);
            point.setBackgroundResource(R.drawable.shape_point_normal);

            // 设置小圆点的布局参数
            int PointSize = getResources().getDimensionPixelSize(R.dimen.point_size);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(PointSize, PointSize);

            if (i > 0) {
                params.leftMargin = getResources().getDimensionPixelSize(R.dimen.point_margin);
                point.setSelected(false);
            } else {
                point.setSelected(true);
            }
            point.setLayoutParams(params);
            // 添加到容器里
            pointGroup.addView(point);
        }

        pointGroup.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                pointGroup.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                if (pointWidth == 0) {
                    pointWidth = pointGroup.getChildAt(1).getLeft() - pointGroup.getChildAt(0).getLeft();
                    Log.d("圆点需要移动的距离：", pointWidth + "");
                }
            }
        });
    }


}
